﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace GUI.NhomHangHoa
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion

        #region Khoi Tao
        private DateTime date;
        private string TenLoaiHang = string.Empty;
        private string MaLoaiHang = string.Empty;
        private Entities.GetCombobox[] phongban = null;
        private string MaNhomHangHoa = string.Empty;
        private string links = string.Empty;
        private string linksRoot = string.Empty;
        private Image image;
        private byte[] bytes;

        private string hanhDong;
        public string HanhDong
        {
            get { return hanhDong; }
            set { hanhDong = value; }
        }
        private Entities.NhomHangHoa nhomhanghoa;

        public Entities.NhomHangHoa Nhomhanghoa
        {
            get { return nhomhanghoa; }
            set { nhomhanghoa = value; }
        }
        public frmXuLy()
        {
            InitializeComponent();
        }
        public frmXuLy(string hanhDong, Entities.NhomHangHoa NhomHangHoa)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
            this.nhomhanghoa = NhomHangHoa;
        }
        #endregion

        #region Load
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            try
            {
                PhongBan();
                date = Common.StaticVariable.InterfaceObject.SelectDate();
                switch (this.hanhDong)
                {
                    case "Them":
                        MaNhomHangHoa = this.GetID();
                        break;
                    case "Sua":
                        DoDuLieu();
                        break;
                    default:
                        this.Thoat();
                        break;
                }
                this.WindowState = FormWindowState.Normal;
                txtTenNhomHang.Focus();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua Du Lieu
        private void DoDuLieu()
        {
            try
            {
                MaNhomHangHoa = nhomhanghoa.MaNhomHang;
                MaLoaiHang = nhomhanghoa.MaLoaiHang;
                TenLoaiHang = nhomhanghoa.TenLoaiHang;
                HienThiPhongBan(nhomhanghoa.MaLoaiHang);
                txtTenNhomHang.Text = nhomhanghoa.TenNhomHang;
                txtghichu.Text = nhomhanghoa.GhiChu;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        CheckClose = true;
                        GetData(false);
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Lay Ma
        private string GetID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "NhomHang";
                Entities.GetID get = Common.StaticVariable.InterfaceObject.SelectID(id);
                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "NH_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "NH_0001";
            }
            return code;
        }
        #endregion

        #region Loại Hàng
        private void HienThiPhongBan(string maloaihang)
        {
            try
            {
                for (int i = 0; i < phongban.Length; i++)
                {
                    if (phongban[i].TenCotKhoaChinh == maloaihang)
                    {
                        cbxTenLoaiHang.SelectedItem = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception)
            {
                cbxTenLoaiHang.Items.Clear();
                MaLoaiHang = string.Empty;
                TenLoaiHang = string.Empty;
            }
        }
        private void PhongBan()
        {
            try
            {
                cbxTenLoaiHang.Items.Clear();
                Entities.GetCombobox pb = new Entities.GetCombobox();
                pb.TenBang = "LoaiHang";
                pb.TenCotKhoaChinh = "MaLoaiHang";
                pb.TenCotTenHienThi = "TenLoaiHang";
                phongban = Common.StaticVariable.InterfaceObject.SelectCombox(pb);
                Common.Utilities.XuLyComboBox(cbxTenLoaiHang, phongban, "TenCotTenHienThi", "TenCotKhoaChinh");
            }
            catch (Exception ex)
            {
                cbxTenLoaiHang.Items.Clear();
                string s = ex.Message;
            }
        }
        #endregion

        #region Quyen
        private Boolean Quyen()
        {
            return true;
        }
        #endregion

        #region Gia Tri Them
        private Entities.NhomHangHoa LayBanGhi(string hanhdong)
        {
            Entities.NhomHangHoa nv = new Entities.NhomHangHoa();
            try
            {
                nv.HanhDong = hanhdong;
                nv.MaNhomHang = MaNhomHangHoa;
                nv.TenNhomHang = txtTenNhomHang.Text.Trim();
                nv.TenLoaiHang = this.TenLoaiHang;
                nv.MaLoaiHang = this.MaLoaiHang;
                nv.GhiChu = txtghichu.Text;
                nv.Deleted = false;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                nv = null;
            }
            return nv;
        }
        #endregion

        #region Validate
        private Boolean Validate(Entities.NhomHangHoa nv)
        {
            Boolean kt = true;
            try
            {
                if (nv.MaNhomHang == string.Empty)
                {
                    MessageBox.Show("Chưa có mã nhóm hàng", "Thông báo");
                    kt = false;
                    return kt;
                }
                if (nv.TenNhomHang == string.Empty)
                {
                    MessageBox.Show("Nhập tên nhóm hàng", "Thông báo");
                    txtTenNhomHang.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.TenLoaiHang == string.Empty)
                {
                    MessageBox.Show("Nhập tên loại hàng", "Thông báo");
                    cbxTenLoaiHang.Focus();
                    kt = false;
                    return kt;
                }
                if (nv.MaLoaiHang == string.Empty)
                {
                    MessageBox.Show("Nhập mã loại hàng", "Thông báo");
                    cbxTenLoaiHang.Focus();
                    kt = false;
                    return kt;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                kt = false;
            }
            return kt;
        }
        #endregion

        #region Them
        private void Them()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền thêm", "Thông báo");
                    return;
                }
                Entities.NhomHangHoa nv = this.LayBanGhi("Insert");
                this.MaNhomHangHoa = this.GetID();
                if (nv.MaNhomHang != this.MaNhomHangHoa)
                {
                    nv = this.LayBanGhi("Insert");
                }
                if (this.Validate(nv) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertUpdateNhomHangHoa(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = true;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua
        private void Sua()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền sửa", "Thông báo");
                    return;
                }
                Entities.NhomHangHoa nv = this.LayBanGhi("Update");
                if (this.Validate(nv) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertUpdateNhomHangHoa(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = false;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region event form
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void btnGhiLai_Click(object sender, EventArgs e)
        {
            try
            {
                switch (this.hanhDong)
                {
                    case "Them":
                        this.Them();
                        break;
                    case "Sua":
                        this.Sua();
                        break;
                }
            }
            catch (Exception)
            {

            }
        }

        private void txtTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            Common.ValidateControl.CheckNumber(e);
        }
        private void cbxPhongBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Entities.GetCombobox row = (Entities.GetCombobox)cbxTenLoaiHang.SelectedItem;
                this.MaLoaiHang = row.TenCotKhoaChinh;
                this.TenLoaiHang = row.TenCotTenHienThi;
            }
            catch (Exception)
            {
                this.MaLoaiHang = string.Empty;
                this.TenLoaiHang = string.Empty;
            }
        }
        #endregion
    }
}
